package com.zzq.guli.service.sms.service.impl;


import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.zzq.guli.service.base.except.GuliException;
import com.zzq.guli.service.base.properties.AliProperties;
import com.zzq.guli.service.base.result.ResultCodeEnum;
import com.zzq.guli.service.base.util.RandomUtils;
import com.zzq.guli.service.sms.service.SmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class SmsServiceIpml implements SmsService {


    @Autowired
    AliProperties aliProperties;

    @Override
    public String getCode(String phoneNumber,String checkCode) throws Exception {

        //初始化配置对象
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(aliProperties.getKeyId())
                // 您的AccessKey Secret
                .setAccessKeySecret(aliProperties.getKeySecret());

        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";


         //创建客户端
        Client client = new Client(config);

        //创建请求对象
        SendSmsRequest request = new SendSmsRequest();
        request.setPhoneNumbers(phoneNumber);
        request.setSignName(aliProperties.getSignName());
        request.setTemplateCode(aliProperties.getTemplateCode());

        request.setTemplateParam("{code:"+checkCode+"}");

        //发送请求
        SendSmsResponse sendSmsResponse = null;
        try {
            sendSmsResponse = client.sendSms(request);
        } catch (Exception e) {
            throw new GuliException(ResultCodeEnum.SMS_ERROR);
        }

        String code = sendSmsResponse.body.getCode();
        return code;
    }
}
